{% extends "base.html" %}

{% block content %}
<div class="row justify-content-center">
    <div class="col-md-6">
        <div class="card shadow">
            <div class="card-header py-3">
                <h5 class="m-0 font-weight-bold text-primary">获取 API Token</h5>
            </div>
            <div class="card-body">
                <form id="tokenForm">
                    <div class="mb-3">
                        <label for="username" class="form-label">用户名</label>
                        <input type="text" class="form-control" id="username" required>
                    </div>
                    <div class="mb-3">
                        <label for="password" class="form-label">密码</label>
                        <input type="password" class="form-control" id="password" required>
                    </div>
                    <div class="d-grid">
                        <button type="submit" class="btn btn-primary">
                            <i class="bi bi-key me-1"></i> 获取 Token
                        </button>
                    </div>
                </form>
                
                <div id="tokenResult" class="mt-4 d-none">
                    <div class="alert alert-success">
                        <h6 class="alert-heading">Token 获取成功</h6>
                        <p>请复制下面的 token 到测试页面使用：</p>
                        <div class="input-group mb-3">
                            <input type="text" id="generatedToken" class="form-control" readonly>
                            <button class="btn btn-outline-secondary" type="button" onclick="copyToken()">
                                <i class="bi bi-clipboard"></i> 复制
                            </button>
                        </div>
                        <div class="d-flex justify-content-between">
                            <button class="btn btn-sm btn-outline-primary" onclick="useTokenInTest()">
                                <i class="bi bi-arrow-left-right me-1"></i> 使用此 Token 测试 API
                            </button>
                            <button class="btn btn-sm btn-outline-secondary" onclick="closeWindow()">
                                <i class="bi bi-x-circle me-1"></i> 关闭窗口
                            </button>
                        </div>
                    </div>
                </div>
                
                <div id="errorResult" class="mt-4 d-none">
                    <div class="alert alert-danger">
                        <h6 class="alert-heading">获取 Token 失败</h6>
                        <p id="errorMessage"></p>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block scripts %}
<script>
    document.addEventListener('DOMContentLoaded', function() {
        const tokenForm = document.getElementById('tokenForm');
        const tokenResult = document.getElementById('tokenResult');
        const errorResult = document.getElementById('errorResult');
        
        tokenForm.addEventListener('submit', function(e) {
            e.preventDefault();
            
            const username = document.getElementById('username').value;
            const password = document.getElementById('password').value;
            
            // 发送请求获取 token
            axios.post('{{ api_prefix }}/auth/token', {
                username: username,
                password: password
            })
            .then(response => {
                const token = response.data.access_token;
                document.getElementById('generatedToken').value = token;
                tokenResult.classList.remove('d-none');
                errorResult.classList.add('d-none');
            })
            .catch(error => {
                let errorMsg = '获取 token 失败';
                if (error.response && error.response.data) {
                    errorMsg = error.response.data.description || errorMsg;
                }
                document.getElementById('errorMessage').textContent = errorMsg;
                errorResult.classList.remove('d-none');
                tokenResult.classList.add('d-none');
            });
        });
    });
    
    function copyToken() {
        const tokenInput = document.getElementById('generatedToken');
        copyToClipboard(tokenInput.value, document.querySelector('#tokenResult button'));
    }
    
    function useTokenInTest() {
        // 将 token 传递给父窗口（测试页面）
        if (window.opener && !window.opener.closed) {
            const token = document.getElementById('generatedToken').value;
            window.opener.postMessage({
                type: 'token_generated',
                token: token
            }, '*');
        }
        window.close();
    }
    
    function closeWindow() {
        window.close();
    }
</script>
{% endblock %}